import store from '@/store'
import config  from './config.js'


let baseUrl = ''

const http = options => {
	const { url, method = 'GET', type = '',json = false,data = {}} = options
	if (type === 'weather')  {
		baseUrl = ''
	} else {
		// #ifdef APP-PLUS
			 baseUrl = config.baseUrl
		// #endif
		// #ifdef H5
			 baseUrl = ''
		// #endif
	}
	return new Promise((relsove,reject) => {
		//console.log(token,'token')
		uni.request({
			url: baseUrl + url,
			method,
			header: {
			 'Content-Type': json ? 'application/json' : 'application/x-www-form-urlencoded',
			  token: store.state.user.userInfo.token     
			},
			data: {
				...data
			},
			success: (response) => {
				
				if (+response.data.code === 200 || +response.data.code === 1) {
					 relsove(response.data)
				}
				
				//用户列表无权限 403
				if(+response.data.code === 403) {
					uni.showToast({
						title: response.data.msg,
						icon: 'none'
					})
					
					reject(response.data.msg)
				}
				//  登录超时
				if (+response.data.code === 401) {
					
					
					 reject(response.data.msg)
				}
				if (+response.data.code !== 1) {
					if (response.data.msg) {
						uni.showToast({
							title: response.data.msg,
							icon: 'none'
						})
					}
					
				}
				reject(response.data)
			},
			fail: (e) =>  {
				reject(e)
			}
		})
	})
}

export default http